package com.zzx.boot.entity.bind;

import com.google.gson.Gson;
import com.zzx.boot.entity.dto.BindLoginDto;
import com.zzx.boot.entity.table.UserBind;
import lombok.AllArgsConstructor;
import org.json.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author tan
 * @version 1.0
 * @date 2023/7/8 10:58
 */
// 第三方微信登录
@AllArgsConstructor
public class WeiXin extends Bind{

    @Resource
    private RestTemplate restTemplate;

    @Override
    public UserBind empower(BindLoginDto loginDto) {
        String json = restTemplate.getForObject("https://api.weixin.qq.com/sns/userinfo?access_token=" + loginDto.getAccessToken() + "&openid=" + loginDto.getOpenid() + "&lang=zh_CN", String.class);
        Map map = new Gson().fromJson(json, Map.class);
        UserBind userBind = new UserBind();
        userBind.setOpenid((String) map.get("openid"));
        userBind.setUserId(0L);
        userBind.setNickname((String) map.get("nickname"));
        userBind.setAvatarurl((String) map.get("headimgurl"));
        userBind.setType("weixin");

        return userBind;
    }
}
